﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ASoft.WPF.ViewModel;
using ASoft.Data;

namespace ASoft.WPF.View
{
    /// <summary>
    /// RoomAllControl.xaml 的交互逻辑
    /// </summary>
    public partial class RoomAllControl : UserControl
    {
        TextBlock txtMessage;
        public RoomAllControl()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(RoomAllControl_Loaded);
            this.Unloaded += new RoutedEventHandler(RoomAllControl_Unloaded);
            txtMessage = txtWS;
        }

        void RoomAllControl_Loaded(object sender, RoutedEventArgs e)
        {
            (new ViewModelLocator()).Main.温室实时数据集合.CollectionChanged +=
                new System.Collections.Specialized.NotifyCollectionChangedEventHandler
                    (温室实时数据集合_CollectionChanged);
            ClearAEllipse();
            AddAllAEllipse();
        }

        void RoomAllControl_Unloaded(object sender, RoutedEventArgs e)
        {
            (new ViewModelLocator()).Main.温室实时数据集合.CollectionChanged -=
              new System.Collections.Specialized.NotifyCollectionChangedEventHandler
                  (温室实时数据集合_CollectionChanged);
        }

        void 温室实时数据集合_CollectionChanged(object sender,
            System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset
                || e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                ClearAEllipse();
            }
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                foreach (RoomDataViewModel room in e.NewItems)
                {
                    AddAEllipse(room);
                }
            }
        }
        private void AddAEllipse(object obj)
        {
            Ellipse ellipse = new Ellipse();
            ellipse.Tag = obj;
            ellipse.DataContext = obj;

            Binding leftBinding = new Binding("Left");
            leftBinding.Mode = BindingMode.OneWay;
            ellipse.SetBinding(Canvas.LeftProperty, leftBinding);

            Binding topBinding = new Binding("Top");
            topBinding.Mode = BindingMode.OneWay;
            ellipse.SetBinding(Canvas.TopProperty, topBinding);

            Binding fillBinding = new Binding("检测结果的颜色");
            fillBinding.Mode = BindingMode.OneWay;
            ellipse.SetBinding(Shape.FillProperty, fillBinding);

            ellipse.MouseLeftButtonUp += new MouseButtonEventHandler(ellipse_MouseLeftButtonUp);

            //<Setter Property="Fill" Value="Red"/>

            ellipseContainer.Children.Add(ellipse);
        }

        void ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var curr = ((Ellipse)sender).Tag as RoomDataViewModel;
            if (curr != null)
            {
                this.CurrentRoom = ((Ellipse)sender).Tag as RoomDataViewModel;
                OnRoomChanged();
            }
        }

        private void AddAllAEllipse()
        {
            foreach (var room in (new ViewModelLocator()).Main.温室实时数据集合)
            {
                AddAEllipse(room);
            }
        }

        private void ClearAEllipse()
        {
            ellipseContainer.Children.Clear();
            var ws = (new ViewModelLocator()).Main.气象数据;
            AddAEllipse(ws);
            ellipseContainer.Children.Add(txtMessage);
        }


        public RoomDataViewModel CurrentRoom { get; private set; }

        public event EventHandler RoomChanged;

        void OnRoomChanged()
        {
            if (RoomChanged != null)
            {
                RoomChanged(this, EventArgs.Empty);
            }
        }
    }
}
